package cn.mayaslight.wechat.utils;

import java.net.URLEncoder;

import cn.mayaslight.common.common.BaseConsts;
import cn.mayaslight.common.exception.HandleException;
import cn.mayaslight.wechat.common.WeChatConsts;

public class URIUtils {

	public static String connectOauth2Authorize(String appid, String redirect_uri, boolean snsapi_userinfo, String state) {
		try {
			StringBuilder sb = new StringBuilder();
			sb.append(WeChatConsts.Constants.OPEN_URI + "/connect/oauth2/authorize?").append("appid=").append(appid).append("&redirect_uri=").append(URLEncoder.encode(redirect_uri, "utf-8")).append("&response_type=code").append("&scope=").append(snsapi_userinfo ? "snsapi_userinfo" : "snsapi_base").append("&state=").append(state == null ? "" : state).append("#wechat_redirect");
			return sb.toString();
		} catch (Exception e) {
			throw new HandleException(e);
		}
	}

	public static String addParameter(String url, String... params) throws Exception {
		StringBuffer sBuf = new StringBuffer(url);
		if (params != null) {
			sBuf.append("?");
			for (int i = 0; i < params.length; i++) {
				String param = params[i];
				if (param.indexOf("=") > -1) {
					String[] split = param.split("=");
					String key = split[0];
					String val = split[1];
					sBuf.append(key).append("=").append(URLEncoder.encode(val, BaseConsts.Constants.CHARSET)).append("&");
				}

			}
			sBuf = sBuf.deleteCharAt(sBuf.length() - 1);
		}
		return sBuf.toString();
	}

	public static String cutOutParameter(String url, String parameterName) {
		if (parameterName.contains("&"))
			throw new RuntimeException("参数名不能包含&字符");
		if (url.indexOf("?") > -1) {
			String queryString = url.substring(url.indexOf("?") + 1, url.length());
			if (queryString.indexOf(parameterName + "=") > -1) {
				String suf = queryString.substring(queryString.indexOf(parameterName + "="), queryString.length());
				if (suf.indexOf("&") > -1) {
					return suf.substring((parameterName + "=").length(), suf.indexOf("&"));
				} else
					return suf.substring((parameterName + "=").length(), suf.length());
			}
		}
		return null;
	}

	public static String deleteParameter(String url, String parameterName) {
		if (parameterName.contains("&"))
			throw new RuntimeException("参数名不能包含&字符");
		if (url.indexOf("?") > -1) {
			String newUrl = url.substring(0, url.indexOf("?") + 1);
			String queryString = url.substring(url.indexOf("?") + 1, url.length());
			if (queryString.indexOf(parameterName + "=") > -1) {
				String pre = queryString.substring(0, queryString.indexOf(parameterName + "="));
				String suf = queryString.substring(queryString.indexOf(parameterName + "="), queryString.length());
				if (suf.indexOf("&") > -1) {
					return newUrl + pre + suf.substring(suf.indexOf("&") + 1, suf.length());
				} else {
					StringBuffer buf = new StringBuffer(newUrl).append(pre);
					return buf.deleteCharAt(buf.length() - 1).toString();
				}
			}
		}
		return url;
	}

}
